package com.zt.struts.action;


import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.zt.exception.InputValidateException;
import com.zt.service.ILeaveService;
import com.zt.data.LeaveVo;

@Controller("/test")
public class TestAction extends DispatchAction {
	
	public ActionForward Leave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		String strDay = request.getParameter("day");
		String reason = request.getParameter("reason");
		request.getSession().setAttribute("userId", request.getParameter("userId"));
		if(StringUtils.isEmpty(strDay) || !StringUtils.isNumeric(strDay)
				|| StringUtils.isEmpty(reason)){
			throw new InputValidateException("Leave day and reason must not be null and reason must be integer");
		}
		
		reason = StringEscapeUtils.escapeHtml(reason);
		LeaveVo leaveVo = new LeaveVo(
				Integer.parseInt(strDay),
				reason.replaceAll("\r\n", "<br>"),
				new Date(),
				(String)request.getSession().getAttribute("userId")
			);
		Long id  = iLeaveService.startLeave(leaveVo);
		
		return mapping.findForward("success");
	}

	public ActionForward myWaitList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String userId = request.getParameter("userId");
		request.setAttribute("list", iLeaveService.myWaitingList(userId));
		return mapping.findForward("taskList");
	}
	
	@Autowired
	private ILeaveService iLeaveService;
	
}
